/**
 * @projectName demo-master
 * @fileName TestController.java
 * @packageName org.example.provider.controller
 * @author xaoyaoyao
 * @date 2021/3/30 10:02
 * @version V1.0
 * @copyright (c) 2021, xaoyaoyao@aliyun.com All Rights Reserved.
 */
package org.example.provider.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.example.provider.feign.Contributor;
import org.example.provider.feign.HelloWorld;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;


/**
 * @author xaoyaoyao
 * @className TestController
 * @description
 * @date 2021/3/30 10:02
 */
@Slf4j
@RestController
@RequestMapping("/provider")
public class TestController {

	@GetMapping(value = "/testFeignApi")
	@ResponseBody
	public HelloWorld testFeignApi(@RequestParam("param") String param) {
		log.debug(param + "我被调用了");
		HelloWorld helloWorld = new HelloWorld();
		helloWorld.setParam(param);
		return helloWorld;
	}

	@PostMapping("/testFeignProviderHasParameter/{id}")
	public HelloWorld testFeignProviderHasParameter(@PathVariable String id) {
		log.debug(id + "我被调用了");
		HelloWorld helloWorld = new HelloWorld();
		helloWorld.setParam(id);
		return helloWorld;
	}

	/**
	 * 接收一个对象
	 *
	 * @param helloWorld
	 *
	 * @return
	 */
	@PostMapping("/testFeignProviderHasParameterB")
	public HelloWorld testFeignApiHasParameterBJson(HelloWorld helloWorld) {
		log.debug(helloWorld + "我被调用了");
		helloWorld.setParam(helloWorld.getParam().toUpperCase() + " : PROVIDER");
		helloWorld.setParam(JSON.toJSONString(helloWorld));
		return helloWorld;
	}

	@PostMapping(value = "/testFeignApiHasParameterBJson", consumes = { MediaType.APPLICATION_JSON_VALUE })
	@ResponseBody
	public HelloWorld testFeignApiHasParameterBJson2(@RequestBody HelloWorld helloWorld) {
		helloWorld.setParam(helloWorld.getParam().toUpperCase() + " : PROVIDER");
		helloWorld.setParam(JSON.toJSONString(helloWorld));
		return helloWorld;
	}

	@GetMapping(value = "/repos/{owner}/{repo}/contributors")
//	@ResponseBody
	public List<Contributor> contributors(@PathVariable("owner") String owner, @PathVariable("repo") String repo) {
		log.debug(owner + ", " + repo + "我被调用了");
		List<Contributor> list        = Lists.newLinkedList();
		Contributor       contributor = new Contributor();
		contributor.setContributions(0);
		contributor.setLogin(owner + ":" + repo);
		list.add(contributor);
		return list;
	}
}
